package com.gxgeek.fastdfsweb.config;

import com.fast.pool.FastdfsPool;
import com.fast.pool.FastdfsPoolConfig;
import com.fast.pool.StorageClient;
import com.gxgeek.fastdfsweb.dto.FastDFSFile;
import lombok.Data;
import org.csource.common.NameValuePair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @author gaoxing
 * @create 2017-11-28 下午4:21
 */
@Component
@Data
public class FastDFSConfig {

//    @Value("#{configProperties['pool.maxActive']}")
    @Value("${pool.maxActive}")
    private int maxActive  ;

//    @Value("#{configProperties['pool.maxIdle']}")
    @Value("${pool.maxIdle}")
    private int maxIdle;

    @Value("${pool.minIdle}")
    private int minIdle;

//    @Value("#{configProperties['pool.maxWait']}")
    @Value("${pool.maxWait}")
    private int maxWait;

//    @Value("#{configProperties['pool.testOnBorrow']}")
    @Value("${pool.testOnBorrow}")
    private boolean testOnBorrow;

//    @Value("#{configProperties['pool.testOnReturn']}")
    @Value("${pool.testOnReturn}")
    private boolean testOnReturn;

//    @Value("#{configProperties['pool.testWhileIdle']}")
    @Value("${pool.testWhileIdle}")
    private boolean testWhileIdle;

//    @Value("#{configProperties['tracker_nginx_host']}")
    @Value("${tracker_nginx_host}")
    private String trackerNginxHost = "localhost";

//    @Value("#{configProperties['tracker_nginx_port']}")
    @Value("${tracker_nginx_port}")
    private String trackerNginxPort = "80";

    /** 协议 */
    private static final String PROTOCOL = "http://";

    /** 路径分隔符 */
    private static final String SEPARATOR = "/";

    /**
     * 初始化配置
     *
     * @return
     */
    private FastdfsPoolConfig initPoolConfig() {
        FastdfsPoolConfig poolCfg = new FastdfsPoolConfig();
        poolCfg.setMaxActive(this.getMaxActive());
        poolCfg.setMaxIdle(this.getMaxIdle());
        poolCfg.setMaxWait(this.getMaxWait());
        poolCfg.setMinIdle(this.getMinIdle());
        poolCfg.setTestOnBorrow(this.isTestOnBorrow());
        poolCfg.setTestOnReturn(this.isTestOnReturn());
        poolCfg.setTestWhileIdle(this.isTestWhileIdle());
        return poolCfg;
    }

    @Bean
    public FastdfsPool fastdfspool(){
        FastdfsPool pool = new FastdfsPool(initPoolConfig(), "fdfs_client.properties");
        return pool;
    }

    /**
     * 组装文件路径
     *
     * @param uploadResults
     * @return
     */
    private String getFileUrl(String[] uploadResults) {
        if (uploadResults != null) {
            String groupName = uploadResults[0];
            String remoteFileName = uploadResults[1];
            StringBuilder sb = new StringBuilder();
            sb.append(PROTOCOL);
            sb.append(this.getTrackerNginxPort());
            sb.append(":");
            sb.append(SEPARATOR);
            sb.append(groupName);
            sb.append(SEPARATOR);
            sb.append(remoteFileName);
            return sb.toString();
        } else {
            return null;
        }
    }

}